import os
import md5
import urllib
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext.webapp import template

from Img import Img

class listimg(webapp.RequestHandler):
    def get(self):
        #get the user
        user = users.get_current_user()

        if not user:
            self.redirect(users.create_login_url(urllib.unquote_plus(self.request.uri)))
            return
        #fetch imgs from db
        imgs = Img.all().filter('uploader = ', user).order('-date')
        templatedic = { 'user': user,
                        'host_url': self.request.host_url,
                        'imgs': imgs,}
        path = os.path.join(os.path.dirname(__file__), 'template', 'list.html')
        self.response.out.write(template.render(path, templatedic))
        return
    
def main():
    application = webapp.WSGIApplication(
                           [('/list/.*', listimg)],
                           debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()

